﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using GSQ.BusinessEntities;
using GSQ.DataAccess.DataSetTableAdapters;

namespace GSQ.DataAccess
{
    public class NcfDA
    {
        private NCFTableAdapter Adapter = new NCFTableAdapter();
        private DataSet.NCFDataTable entities;
        private DataSet.NCFRow entity;

        public decimal Insert(NCF value)
        {
            throw new NotImplementedException();
            //using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            //{
            //    try
            //    {
            //        cnn.Open();
            //        Adapter.Connection = cnn;

            //        entities = new DataSet.NCFDataTable();

            //        entity = entities.NewNCFRow();

            //        entity.ID = value.ID;
            //        entity.NCF = value.NCF1.Trim();
            //        entity.DESCRIPCION = value.DESCRIPCION;
            //        entity.TIPO_NCF = value.TIPO_NCF;
            //        entity.ID_CONDICION = value.ID_CONDICION;
            //        entity.DESDE = value.DESDE;
            //        entity.HASTA = value.HASTA;

            //        entity.ACTIVO = value.ACTIVO;

            //        entities.AddVENDEDORESRow(entity);
            //        Adapter.Update(entities);

            //        value.ID = entity.ID;
            //        return value.ID;
            //    }
            //    finally
            //    {
            //        if (cnn.State != System.Data.ConnectionState.Closed)
            //            cnn.Close();
            //    }
            //}
        }

        public decimal Update(NCF value)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    entities = Adapter.GetDataByID(value.ID);
                    entity = entities.FirstOrDefault();

                    entity.NCF = value.NCF1;
                    entity.DESCRIPCION = value.DESCRIPCION;
                    entity.TIPO_NCF = value.TIPO_NCF;
                    entity.ID_CONDICION = value.ID_CONDICION;
                    entity.DESDE = value.DESDE;
                    entity.HASTA = value.HASTA;
                    entity.RNC_REQUERIDO = value.RNC_REQUERIDO;
                    entity.ACTIVO = value.ACTIVO;

                    this.Adapter.Update(entities);

                    return entity.ID;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public NCF GetById(decimal id)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;
                    return this.GetEntity(this.Adapter.GetDataByID(id).FirstOrDefault());
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<NCF> GetAll()
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<NCF> lst = new List<NCF>();
                    foreach (DataSet.NCFRow rw in this.Adapter.GetData())
                    {
                        lst.Add(GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        public List<NCF> GetByIdCondicionTipo(decimal idCondicion, string tipo)
        {
            using (SqlConnection cnn = new SqlConnection(Connection.GetConnectionString))
            {
                try
                {
                    cnn.Open();
                    this.Adapter.Connection = cnn;

                    List<NCF> lst = new List<NCF>();
                    foreach (DataSet.NCFRow rw in this.Adapter.GetDataByTipoIDCondicion(tipo, idCondicion))
                    {
                        lst.Add(GetEntity(rw));
                    }
                    cnn.Close();
                    return lst;
                }
                finally
                {
                    if (cnn.State != System.Data.ConnectionState.Closed)
                        cnn.Close();
                }
            }
        }

        private NCF GetEntity(DataSet.NCFRow rw)
        {
            NCF en = null;
            if (rw != null)
            {
                en = new NCF();
                en.ID = rw.ID;
                en.NCF1 = rw.NCF;
                en.DESCRIPCION = rw.DESCRIPCION;
                en.TIPO_NCF = rw.TIPO_NCF;
                en.ID_CONDICION = rw.ID_CONDICION;
                en.DESDE = rw.DESDE;
                en.HASTA = rw.HASTA;
                en.VALUE = rw.VALUE;
                en.ACTIVO = rw.ACTIVO;
                en.RNC_REQUERIDO = rw.RNC_REQUERIDO;
                en.IMPUESTOS = rw.IMPUESTOS;
                en.CONDICIONES = new CondicionesDA().GetById(en.ID_CONDICION);
            }
            return en;
        }
    }
}
